<?php
//http://example.com/[controller-class]/[controller-method]/[arguments]

defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {	 

	public function __construct() {
		parent::__construct();
		$this->load->model('user_model');
		$this->load->helper('url_helper');
		$this->load->library('vexuser');
	}
	
	public function register() {
		if ($this->vexuser->is_logged_in() != False) {
			$data['notice'] = 'You have logged in.<br /><a href="logout">Logout</a>';
			$this->load->view('users/register_notice', $data);
			return;
		}
		
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_rules('email', 'E-Mail', 'required');
		$this->form_validation->set_rules('nickname', 'Nickname', 'required');
		$this->form_validation->set_rules('utype', 'Usertype', 'in_list[user,vul]');
		
		if ($this->form_validation->run() === FALSE) {
			# $this->load->view('templates/header');
			$data = "Validation failed.<br />";
			$this->load->view('users/register', $data);
			# $this->load->view('templates/footer');
		} else {
			$res_utype = $this->input->post('utype');
			if ($res_utype == "user") {
				$res_utype_data = UTYPE_USER;
			} else if ($res_utype == "vul") {
				$res_utype_data = UTYPE_UNAUTH_VOLUNTEER;
			} else {
				# $this->load->view('templates/header');
				$data['notice'] = "Not in list:vul|user";
				$this->load->view('users/register', $data);
				# $this->load->view('templates/footer');
				return;
			}
			$ret = $this->user_model->add_user(array('name' => $this->input->post('name'), 
										  'password' => $this->input->post("password"),
										  'email' => $this->input->post("email"),
										  'nickname' => $this->input->post("nickname"),
										  'ustc_gid' => 0,
										  'utype' => $res_utype_data
										  ));
			if ($ret == True) {
				$data['notice'] = "You have successfully registered.";
				$this->load->view('users/register_notice', $data);
			} else {
				$data['notice'] = "Error: duplicated username";
				$this->load->view('users/register', $data);
			}
		}
	}

	public function login() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		if ($this->vexuser->is_logged_in() != False) {
			$data['notice'] = 'You have already logged in.<br /><a href="logout">Logout</a>';
			$this->load->view('users/login_notice', $data);
			return;
		}
		
		$this->form_validation->set_rules('name', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		if ($this->form_validation->run() === FALSE) {
			$this->load->view("users/login");
		} else {
			$ret = $this->user_model->check_password($this->input->post("name"),$this->input->post("password"));
			if ($ret === False) {
				$data['notice'] = "Wrong username/password combination";
				$this->load->view('users/login', $data);
			} else {
				$this->vexuser->set_auth_session($ret["uid"], $ret["utype"]);
				$data['notice'] = "You have successfully logged in. UID:". $ret["uid"] . "<br />Identity:". $ret["utype"];
				$this->load->view('users/login_notice', $data);
			}
		}
	}
	
	public function logout() {
		if ($this->vexuser->is_logged_in() == False) {
			$data['notice'] = 'You have not logged in.<br /><a href="login">Login</a>';
			$this->load->view('users/login_notice', $data);
			return;
		}
		$this->vexuser->destroy_auth_session();
		$data['notice'] = 'You have logged out.<br /><a href="login">Login</a>';
		$this->load->view('users/login_notice', $data);
		
	}
	
	public function show_profile() {
		$uid = $this->vexuser->is_logged_in();
		if ($uid == False) {
			$data['notice'] = 'You have not logged in.<br /><a href="login">Login</a>';
			$this->load->view('users/login_notice', $data);
			return;
		}
		$query = $this->user_model->get_user_info($uid);

		$data['info'] = $query;
		# Find the user's vexation
		$vquery = $this->user_model->get_user_vex($uid);
		$data['vexq'] = $vquery;

		$this->load->view('users/profile', $data);
	}
	
	public function show_user_list() {
		$query = $this->user_model->get_user_list();
		$data['query'] = $query;
		$this->load->view('users/user_list', $data);
	}	
}
